
#ifdef ASPECTS_EXPORTS
#define ASPECTS_API __declspec(dllexport)
#else
#define ASPECTS_API __declspec(dllimport)
#endif

#ifndef ASPECTS_H
#define ASPECTS_H

#include "aop.h"
#include "myaspects_dac.h"

#include "..\Infobus\Infobus.h"
#include "..\Infobus\Types.h"

#include <comdef.h>

class ASPECTS_API TraceAllAspect : public Aspect{
public:
  TraceAllAspect()
  : m_pDisp(NULL)
  {
    m_pDisp = new Dispatcher(TRACES);

    memset(szExepath, 0, sizeof szExepath);
    memset(szThreadId, 0, sizeof szThreadId);
    memset(szProcessId, 0, sizeof szProcessId);
  }
  ~TraceAllAspect()
  {
    if (m_pDisp) delete m_pDisp;
  }
  void advice()
  {
    _bstr_t msg = "<trace>"
                  "  <component>"
                  "    <filename/>"
                  "    <lineno/>"
                  "    <executable>"+_bstr_t(getExecFileName())+"</executable>"
                  "    <processid>"+_bstr_t(::GetCurrentProcessId())+"</processid>"
                  "    <threadid>"+_bstr_t(::GetCurrentThreadId())+"</threadid>"
                  "  </component>"
                  "  <object>"
                  "    <class>"+_bstr_t(GetJPClassName().c_str())+"</class>"
                  "    <method event=\"entry\">"
                          +_bstr_t(GetJPMethodName().c_str())+
                  "    </method>"
                  "  </object>"
                  "</trace>";

    if (m_pDisp) m_pDisp->sendMessage(msg);
  }
protected:
  const char* getExecFileName()
  {
    if(0 != GetModuleFileName(0, szExepath, MAX_PATH))
      ::OutputDebugString("* Unable to get filename of executable");
    return szExepath;
  }
  const char* getThreadId()
  {
    ltoa(::GetCurrentThreadId(), szThreadId, 10);
    return szThreadId;
  }
  const char* getProcessId()
  {
    ltoa(::GetCurrentProcessId(), szProcessId, 10);
    return szProcessId;
  }
private:
  Dispatcher * m_pDisp;

  char szExepath[MAX_PATH+1];
  char szThreadId[16];
  char szProcessId[16];
};



#endif
